package com.kadron.library.item;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.kadron.creator.KItemModelCreator;
import com.kadron.dao.KCommonDAO;
import com.kadron.dao.KDiscountItemDAO;
import com.kadron.dao.KImageDAO;
import com.kadron.dao.KItemDAO;
import com.kadron.entity.Color;
import com.kadron.entity.Material;
import com.kadron.entity.Size;
import com.kadron.item.AbstractItemManager;
import com.kadron.item.KItemListSearch;
import com.kadron.paging.ItemsHolder;
import com.kadron.paging.ItemsModelContainer;
import com.kadron.paging.KItemRowService;
import com.kadron.session.KUser;

@AutoCreate
@Scope(ScopeType.PAGE)
@Name("kItemListViewManager")
public class KItemListViewManager extends AbstractItemManager implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4977523492652008184L;
	
//	private KDataModel itemList;
//	private KILModelDataProvider dataProvider;
	private KItemListSearch search;
	private ItemsHolder itemsHolder;
	
	private List<Color> acolors = new LinkedList<Color>();
	private List<Size> asizes = new LinkedList<Size>();
	private List<Material> amaterials = new LinkedList<Material>();
	
	@In
	private KCommonDAO commonDAO;
	@In
	private KItemDAO itemDAO;
	@In
	private KDiscountItemDAO discountItemDAO;
	@In
	private KImageDAO imageDAO;
	
	@Create
	public void init() {
		try {
			KUser kUser = (KUser) Component.getInstance("kUser");
			acolors = commonDAO.getAll(Color.class);
			asizes = commonDAO.getAll(Size.class);
			amaterials = commonDAO.getAll(Material.class);
			search = new KItemListSearch();
			if (kUser.getCurrency() != null) {
				search.setCurrencyId(kUser.getCurrency().getId());
			}
			itemsHolder = new ItemsHolder(new ItemsModelContainer(6), new KItemRowService(new KItemModelCreator(itemDAO, discountItemDAO, imageDAO), search, itemDAO));
			search();			
		} catch (Exception e) {
			e.printStackTrace();
			super.setError(true);
		}
	}
	
	public void search() {
		itemsHolder = new ItemsHolder(new ItemsModelContainer(6), new KItemRowService(new KItemModelCreator(itemDAO, discountItemDAO, imageDAO), search, itemDAO));
		itemsHolder.setRowService(new KItemRowService(new KItemModelCreator(itemDAO, discountItemDAO, imageDAO), search, itemDAO));
	}
	
	public void reset() {
		KUser kUser = KUser.instance();
		search = new KItemListSearch(kUser.getCurrency().getId());
		itemsHolder.setRowService(new KItemRowService(new KItemModelCreator(itemDAO, discountItemDAO, imageDAO), search, itemDAO));
	}
	
	public KItemListSearch getSearch() {
		return search;
	}

	public boolean isCC() {
		KUser kUser = KUser.instance();
		if (kUser.getCurrency().getId() != search.getCurrencyId()) {
			reset();
			return true;
		}
		return false;
	}

	public ItemsHolder getItemsHolder() {
		return itemsHolder;
	}

	public List<Color> getAcolors() {
		return acolors;
	}

	public List<Size> getAsizes() {
		return asizes;
	}

	public List<Material> getAmaterials() {
		return amaterials;
	}
	
}
